<script setup lang="ts">
import { ref } from "vue";
import { PureTableBar } from "@/components/RePureTableBar";
import { useRenderIcon } from "@/components/ReIcon/src/hooks";
import { useSystemConfig } from "./utils/hook";
import EditPen from "~icons/ep/edit-pen";
import Plus from "~icons/ep/plus";
import Delete from "~icons/ep/delete";

defineOptions({ name: "SystemConfig" });

const tableRef = ref();
const {
  filterForm,
  categories,
  statusOptions,
  pagination,
  tableData,
  columns,
  loading,
  handleSearch,
  resetFilters,
  handlePageChange,
  handleSizeChange,
  openCreate,
  openEdit,
  handleDelete,
  serialiseValue,
  previewValue,
  shouldExpandValue
} = useSystemConfig();
</script>

<template>
  <div class="config-page">
    <el-form
      :inline="true"
      :model="filterForm"
      class="search-form bg-bg_color w-full pl-4 pt-3 pb-2 overflow-auto"
    >
      <el-form-item label="配置键：" prop="keyword">
        <el-input
          v-model="filterForm.keyword"
          placeholder="配置键或描述"
          clearable
          class="w-[220px]!"
        />
      </el-form-item>
      <el-form-item label="分类：" prop="category">
        <el-select
          v-model="filterForm.category"
          placeholder="全部分类"
          clearable
          class="w-[180px]!"
        >
          <el-option label="全部分类" value="" />
          <el-option
            v-for="item in categories"
            :key="item"
            :label="item"
            :value="item"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态：" prop="status">
        <el-select
          v-model="filterForm.status"
          placeholder="全部状态"
          clearable
          class="w-[140px]!"
        >
          <el-option
            v-for="item in statusOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          :icon="useRenderIcon('ri:search-line')"
          :loading="loading"
          @click="handleSearch"
        >
          搜索
        </el-button>
        <el-button :icon="useRenderIcon('ep:refresh')" @click="resetFilters">
          重置
        </el-button>
      </el-form-item>
    </el-form>

    <PureTableBar
      title="配置管理"
      :columns="columns"
      :tableRef="tableRef"
      @refresh="handleSearch"
      @fullscreen="tableRef.value?.setAdaptive?.()"
    >
      <template #buttons>
        <el-button
          type="primary"
          :icon="useRenderIcon(Plus)"
          @click="openCreate"
        >
          新增配置
        </el-button>
      </template>
      <template #default="slotProps">
        <PureTable
          ref="tableRef"
          v-loading="loading"
          row-key="id"
          :columns="columns"
          :data="tableData"
          :size="slotProps?.size ?? 'default'"
          :pagination="pagination"
          adaptive
          :adaptiveConfig="{ offsetBottom: 48 }"
          @page-current-change="handlePageChange"
          @page-size-change="handleSizeChange"
        >
          <template #type="{ row }">
            <el-tag size="small" type="info" effect="plain">
              {{ row.value_type }}
            </el-tag>
          </template>
          <template #value="{ row }">
            <el-popover
              v-if="shouldExpandValue(row.config_value)"
              placement="top-start"
              trigger="click"
              width="420"
            >
              <template #reference>
                <span class="config-preview">
                  {{ previewValue(row.config_value) }}
                  <el-icon class="config-preview__icon">
                    <i class="i-ep-view" />
                  </el-icon>
                </span>
              </template>
              <pre class="config-json"
                >{{ serialiseValue(row.config_value) }}
              </pre>
            </el-popover>
            <span v-else>{{ previewValue(row.config_value) }}</span>
          </template>
          <template #status="{ row }">
            <el-tag
              :type="row.status === 1 ? 'success' : 'danger'"
              size="small"
              effect="plain"
            >
              {{ row.status === 1 ? "启用" : "停用" }}
            </el-tag>
          </template>
          <template #operation="{ row }">
            <el-button
              class="reset-margin"
              link
              type="primary"
              :icon="useRenderIcon(EditPen)"
              @click="openEdit(row)"
            >
              编辑
            </el-button>
            <el-popconfirm
              :title="`确认删除配置「${row.config_key}」吗？`"
              @confirm="handleDelete(row)"
            >
              <template #reference>
                <el-button
                  class="reset-margin"
                  link
                  type="danger"
                  :icon="useRenderIcon(Delete)"
                >
                  删除
                </el-button>
              </template>
            </el-popconfirm>
          </template>
        </PureTable>
      </template>
    </PureTableBar>
  </div>
</template>

<style scoped>
.config-page {
  padding: 12px 12px 20px;
}

.search-form :deep(.el-form-item) {
  margin-bottom: 12px;
}

.search-form :deep(.el-form-item__label) {
  padding-right: 6px;
}

.config-json {
  margin: 0;
  max-height: 240px;
  overflow: auto;
  font-family: "Menlo", "Monaco", "Courier New", monospace;
  font-size: 12px;
  line-height: 1.6;
  white-space: pre-wrap;
  word-break: break-all;
}

.config-preview {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  color: var(--el-color-primary);
  cursor: pointer;
  line-height: 20px;
}

.config-preview__icon {
  font-size: 14px;
}
</style>
